package com.nullpointer.playerinfo.common.playerInfoAPI;

import java.sql.SQLException;
import java.util.UUID;

import com.nullpointer.playerinfo.bukkit.PlayerInfoBukkit;
import com.nullpointer.playerinfo.common.Base;
import com.nullpointer.playerinfo.common.objects.Player;


/**
 * 此类中的方法供外部使用
 * 注意所有涉及数据库操作的方法都需要异步调用
 */


public class bukkit {
    
    /**
     * 用于重置指定UUID的玩家的数据
     * 注意：仅重置金币、等级、经验值，其他保持不变
     */

    public static void removeData(UUID UUID) throws SQLException {
        if (PlayerInfoBukkit.bungeeMode) {
            PlayerInfoBukkit.correspond.sendMessage("removeData " + UUID);
        } else {
            Player player = Base.playerMap.get(UUID);
            if (player == null) {
                player = PlayerInfoBukkit.mysql.getPlayer(UUID);
            }
            player.removeData();
        }
    }


    /**
     * 用于通过UUID获取玩家实例
     * 注意返回对象并非bukkit的player
     */
    @Deprecated
    public static Player getPlayer(UUID UUID) throws SQLException {
        Player player = Base.playerMap.get(UUID);
        if (player != null) {
            return player;
        } else {
            return PlayerInfoBukkit.mysql.getPlayer(UUID);
        }
    }

    /**
     * 用于获取玩家的首次登录时间
     */

    public static Long getFirstPlayTime(UUID UUID) throws SQLException {
        Player player = Base.playerMap.get(UUID);
        if (player != null) {
            return player.getFirstPlayTime();
        } else {
            return PlayerInfoBukkit.mysql.getPlayer(UUID).getFirstPlayTime();
        }
    }

    /**
     * 用于获取玩家的上一次的登录时间
     */

    public static Long getLastPlayTime(UUID UUID) throws SQLException {
        Player player = Base.playerMap.get(UUID);
        if (player != null) {
            return player.getLastPlayTime();
        } else {
            return PlayerInfoBukkit.mysql.getPlayer(UUID).getLastPlayTime();
        }
    }

    /**
     * 获取玩家的首次登录时间并格式化成：年/月/日
     */

    public static String getFirstPlayTimeFormatted(UUID UUID) throws SQLException {
        Player player = Base.playerMap.get(UUID);
        if (player != null) {
            return player.getFirstPlayTimeFormatted();
        } else {
            return PlayerInfoBukkit.mysql.getPlayer(UUID).getFirstPlayTimeFormatted();
        }
    }

    /**
     * 获取玩家的上一次登录时间并格式化成：年/月/日
     */

    public static String getLastPlayTimeFormatted(UUID UUID) throws SQLException {
        Player player = Base.playerMap.get(UUID);
        if (player != null) {
            return player.getLastPlayTimeFormatted();
        } else {
            return PlayerInfoBukkit.mysql.getPlayer(UUID).getLastPlayTimeFormatted();
        }
    }

    /**
     * 获取玩家的总游戏时长，单位为秒
     */

    public static int getTotalOnlineTime(UUID UUID) throws SQLException {
        Player player = Base.playerMap.get(UUID);
        if (player != null) {
            return player.getTotalOnlineTime();
        } else {
            return PlayerInfoBukkit.mysql.getPlayer(UUID).getTotalOnlineTime();
        }
    }

    /**
     * 获取玩家上一次的游戏时长，单位为秒
     */

    public static int getOnlineTime(UUID UUID) throws SQLException {
        Player player = Base.playerMap.get(UUID);
        if (player != null) {
            return player.getOnlineTime();
        } else {
            return PlayerInfoBukkit.mysql.getPlayer(UUID).getOnlineTime();
        }
    }

    /**
     * 获取玩家上一次的游戏时长并格式化，单位为小时
     */

    public static String getOnlineTimeFomatted(UUID UUID) throws SQLException {
        Player player = Base.playerMap.get(UUID);
        if (player != null) {
            return player.getOnlineTimeFomatted();
        } else {
            return PlayerInfoBukkit.mysql.getPlayer(UUID).getOnlineTimeFomatted();
        }
    }

    /**
     * 获取玩家的总游戏时长并格式化，单位为小时
     */

    public static Long getTotalOnlineTimeFomatted(UUID UUID) throws SQLException {
        Player player = Base.playerMap.get(UUID);
        if (player != null) {
            return player.getTotalOnlineTimeFomatted();
        } else {
            return PlayerInfoBukkit.mysql.getPlayer(UUID).getTotalOnlineTimeFomatted();
        }
    }

    /**
     * 获取玩家的当前的经验值
     */

    public static double getExp(UUID UUID) throws SQLException {
        Player player = Base.playerMap.get(UUID);
        if (player != null) {
            return player.getExp();
        } else {
            return PlayerInfoBukkit.mysql.getPlayer(UUID).getExp();
        }
    }

    /**
     * 获取玩家当前的等级
     */

    public static double getLevel(UUID UUID) throws SQLException {
        Player player = Base.playerMap.get(UUID);
        if (player != null) {
            return player.getLevel();
        } else {
            return PlayerInfoBukkit.mysql.getPlayer(UUID).getLevel();
        }
    }

    /**
     * 获取玩家当前的金币数量
     */

    public static double getCash(UUID UUID) throws SQLException {
        Player player = Base.playerMap.get(UUID);
        if (player != null) {
            return player.getCash();
        } else {
            return PlayerInfoBukkit.mysql.getPlayer(UUID).getCash();
        }
    }

    /**
     * 为玩家增加经验值
     * 注意判别升级后是否超过level.yml中设置的最高等级
     */

    public static void addExp(UUID UUID, int exp) throws SQLException {
        Player player = Base.playerMap.get(UUID);
        if (player != null) {
            player.addExp(exp);
        } else {
            PlayerInfoBukkit.mysql.getPlayer(UUID).addExp(exp);
            PlayerInfoBukkit.mysql.savePlayer(player);
        }
    }

    /**
     * 为玩家增加金币
     */

    public static void addCash(UUID UUID, int cash) throws SQLException {
        Player player = Base.playerMap.get(UUID);
        if (player != null) {
            player.addCash(cash);
        } else {
            PlayerInfoBukkit.mysql.getPlayer(UUID).addCash(cash);
            PlayerInfoBukkit.mysql.savePlayer(player);
        }
    }

    /**
     * 扣除玩家的金币
     */

    public static void removeCash(UUID UUID, int cash) throws SQLException {
        Player player = Base.playerMap.get(UUID);
        if (player != null) {
            player.removeCash(cash);
        } else {
            PlayerInfoBukkit.mysql.getPlayer(UUID).removeCash(cash);
            PlayerInfoBukkit.mysql.savePlayer(player);
        }
    }
}
